import * as couponService from '../services/memberService';
import { decryptRes, dealPagination } from '../utils/requestUtil';
import { fetchChannelTree } from '../services/contentService';
import { message } from 'antd';

export default {
  namespace: 'coupon',

  state: {
    couponList: [],
    pagination: {},

    couponDetail: null,

    goodsList: [],
    channelTree: [],

  },

  effects: {
    *fetchCouponList({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(couponService.fetchCouponList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'couponListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchCouponDetail({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(couponService.fetchCouponDetail, payload)));
      console.log('接口返回数据11111111：', response.data);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'couponDetailReducer',
          payload: { data },
        });
      
        if (data && data.requiredAmt && (data.requiredAmt > 0)) {
          data['amtType'] = "1"
        } else {
          data['amtType'] = "0"
        }

        if (callback) callback(data);
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchGoodsList({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(couponService.fetchGoodsList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'goodsListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchChannelTree({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(fetchChannelTree, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        let channelTree = [];
        let one = null;

        for (let i = 0; i < data.length; i++) {
          one = {};
          one["title"] = (data[i].appType == '1' ? '小萌早教' :
            data[i].appType == '2' ? '小萌绘本' : data[i].appType == '3' ? '萌萌乐视频' :
              data[i].appType == '4' ? '萌萌乐儿歌' :
                '其他');
          one['key'] = '' + data[i].appType;
          one['value'] = '' + data[i].appType;
          one['disableCheckbox'] = true;

          if (data[i].channelList && (data[i].channelList.length > 0)) {
            let children = [];
            for (let j = 0; j < data[i].channelList.length; j++) {
              children.push({ "title": data[i].channelList[j].channelName, "key": '' + data[i].channelList[j].channelId, "value": '' + data[i].channelList[j].channelId });
            }
            if (children && children.length > 0) {
              one['children'] = children;
            }
          }
          if (one) {
            channelTree.push(one)
          }
        }

        yield put({
          type: 'channelTreeReducer',
          payload: { data: channelTree },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *addCoupon({ payload, callback }, { call, put }) {
      console.log('addCoupon前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(couponService.addCoupon, payload)));
      console.log('addCoupon接口返回数据：', response);

      if (response.code === 0) {
        message.success('添加成功！')
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *editCoupon({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(couponService.editCoupon, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        message.success('修改成功！')
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *deleteCoupon({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(couponService.deleteCoupon, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;

        message.success('删除成功！')

        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
  },

  reducers: {
    couponListReducer(state, action) {
      return {
        ...state,
        couponList: action.payload.data.dataList,
        pagination: dealPagination(action.payload.data.pageInfo),
      };
    },
    couponDetailReducer(state, action) {
      console.log('couponDetailReducercouponDetailReducer??', action.payload.data)
      return {
        ...state,
        couponDetail: action.payload.data,
      };
    },
    goodsListReducer(state, action) {
      return {
        ...state,
        goodsList: action.payload.data,
      };
    },
    channelTreeReducer(state, action) {
      console.log('channelTree-----', action.payload.data)
      return {
        ...state,
        channelTree: action.payload.data,
      };
    },
  },
};
